using OracleInternal.Json.Binary;

namespace OracleInternal.Json.Sql
{
	internal class OracleJsonBinary : OracleJsonValue
	{
		protected static char[] HEX_UPPER = "0123456789ABCDEF".ToCharArray();

		protected static char[] HEX_LOWER = "0123456789abcdef".ToCharArray();

		private byte[] m_bytes;

		private bool m_isId;

		internal OracleJsonBinary(byte[] bytes, bool isId)
		{
			m_bytes = bytes;
			m_isId = isId;
		}

		public override OracleJsonType GetOracleJsonType()
		{
			return OracleJsonType.BINARY;
		}

		internal bool IsId()
		{
			return m_isId;
		}

		public override string ToJsonString()
		{
			return OracleJsonSerializer.SerializeString(GetJsonString());
		}

		internal byte[] GetBytes()
		{
			return m_bytes;
		}

		internal string GetJsonString()
		{
			return GetJsonString(m_bytes, m_isId);
		}

		internal static string GetJsonString(byte[] bytes, bool isId)
		{
			return new string(SerializeBinary(bytes, isId ? HEX_LOWER : HEX_UPPER));
		}

		private static char[] SerializeBinary(byte[] bytes, char[] hex)
		{
			char[] array = new char[bytes.Length * 2];
			int num = 0;
			int num2 = 0;
			while (num < bytes.Length)
			{
				byte b = bytes[num++];
				array[num2++] = hex[(b >> 4) & 0xF];
				array[num2++] = hex[b & 0xF];
			}
			return array;
		}
	}
}
